<?php
class ModelCatalogNews extends Model
{
	public function addNews($data)
	{
		$this->db->query("INSERT INTO ".DB_PREFIX."news SET date_added=NOW(),date_modified=NOW(), image='".$this->db->escape($data['image'])."', category_id='".(int)$data['category_id']."',  status='".(int)$data['status']."', sort_order='".(int)$data['sort_order']."'");
		$news_id=$this->db->getLastId();
		foreach ($data['news_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "news_description SET news_id = '" . (int)$news_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "', summary='".$this->db->escape($value['summary'])."'");			
		}
		
		
		
						
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'news_id=" . (int)$news_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
						
	
	}
	public function editNews($news_id,$data)
	{
		$this->db->query("UPDATE ".DB_PREFIX."news SET date_modified=NOW(), image='".$this->db->escape($data['image'])."', status='".(int)$data['status']."', sort_order='".(int)$data['sort_order']."', category_id='".(int)$data['category_id']."' WHERE news_id='".(int)$news_id."'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$news_id . "'");
	
		foreach ($data['news_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "news_description SET news_id = '" . (int)$news_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "', summary='".$this->db->escape($value['summary'])."'");
		}	
		
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$news_id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'news_id=" . (int)$news_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}	
		
	}
	
	public function deleteNews($news_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "news WHERE news_id = '" . (int)$news_id . "'");		
		$this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$news_id . "'");		
				
	
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$news_id. "'");
		
		
	}
	public function getNews($news_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$news_id . "') AS keyword FROM " . DB_PREFIX . "news p LEFT JOIN " . DB_PREFIX . "news_description pd ON (p.news_id = pd.news_id)  WHERE p.news_id = '" . (int)$news_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
				
		return $query->row;
	}
	public function getNewss($data = array()) {
		if ($data) {
			$sql = "SELECT *, i.news_id FROM " . DB_PREFIX . "news i LEFT JOIN " . DB_PREFIX . "news_description id ON (i.news_id = id.news_id) ";
			
			
					
			$sql .= " WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "'"; 
			
			
			if (!empty($data['filter_title'])) {
				$sql .= " AND LCASE(id.title) LIKE '%" . $this->db->escape(utf8_strtolower($data['filter_title'])) . "%'";
			}

			if (!empty($data['filter_date_added'])) {
				$sql .= " AND i.date_added = '" . date(('Y-m-d'),strtotime($data['filter_date_added'])) . "'";
			}
			
			
			
			if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
				$sql .= " AND i.status = '" . (int)$data['filter_status'] . "'";
			}
					
			
			
			$sql .= " GROUP BY i.news_id";
						
			$sort_data = array(
				'id.title',				
				'i.date_added',				
				'i.status',
				'i.sort_order'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY i.news_id";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'ADC')) {
				$sql .= " ASC";
			} else {
				$sql .= " DESC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
		
			return $query->rows;
		} else {
			$news_data = $this->cache->get('news.' . (int)$this->config->get('config_language_id'));
		
			if (!$news_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news p  ORDER BY p.news_id DESC");
	
				$news_data = $query->rows;
			
			
			}	
	
			return $news_data;
		}
	}
	
	public function getNewsDescriptions($news_id) {
		$news_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$news_id . "'");
		
		foreach ($query->rows as $result) {
			$news_description_data[$result['language_id']] = array(
				'title'             => $result['title'],
				'summary'             => $result['summary'],
				'description'      => $result['description'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description']
			);
		}
		
		return $news_description_data;
	}
	
	
	

	
	
	public function getNewsTags($news_id) {
		$news_tag_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_tag WHERE news_id = '" . (int)$news_id . "'");
		
		$tag_data = array();
		
		foreach ($query->rows as $result) {
			$tag_data[$result['language_id']][] = $result['tag'];
		}
		
		foreach ($tag_data as $language => $tags) {
			$news_tag_data[$language] = implode(',', $tags);
		}
		
		return $news_tag_data;
	}
	
	public function getTotalNewss($data = array()) {
		$sql = "SELECT COUNT(DISTINCT p.news_id) AS total FROM " . DB_PREFIX . "news p LEFT JOIN " . DB_PREFIX . "news_description pd ON (p.news_id = pd.news_id) ";

		
		 
		$sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		
		
		 			
		if (!empty($data['filter_title'])) {
			$sql .= " AND LCASE(pd.title) LIKE '%" . $this->db->escape(utf8_strtolower($data['filter_title'])) . "%'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND p.date_added = '" . date('Y-m-d',strtotime($data['filter_date_added'])) . "'";
		}
		
		
		
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
		}

		
		
		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}
	
	
	
}
?>